/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.server.disposer;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.Collection;
import org.toools.gwt.wmc.client.disposer.DisposerService;
import org.toools.gwt.wmc.server.dao.IDisposerDAO;
import org.toools.gwt.wmc.jpa.Disposer;
import org.toools.gwt.wmc.server.WMCSession;

/**
 *
 * @author Manuel
 */
public class DisposerServiceImpl extends RemoteServiceServlet implements DisposerService {

    private IDisposerDAO disposerdao;

    /** Account DAO **/
    public void setDisposerdao( IDisposerDAO dao ) {
        this.disposerdao = dao;
    }
    public IDisposerDAO getDisposerdao() {
		return disposerdao;
	}


    public Disposer getDisposerByID(Integer ID) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || disposerdao.belongsTo( ID, WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return disposerdao.getDisposerByID( ID );

        return null;
    }

    public Collection<Disposer> getDisposers() {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return disposerdao.getDisposers();

        return disposerdao.getDisposers( WMCSession.getAccountID( getThreadLocalRequest() ) );
    }

    public Disposer create(Disposer d) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return disposerdao.create( d );
        //check if belongs to account
        else
            return disposerdao.create( d );
    }

    public Boolean update(Disposer d) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || disposerdao.belongsTo( d.getId(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return disposerdao.update( d );
        return false;
    }

    public Boolean delete(Disposer d) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || disposerdao.belongsTo( d.getId(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return disposerdao.delete( d );
        return false;
    }
}
